<?php

// swoole多线程网络爬虫了解一下！

echo '开始时间:' . date('H:i:s', time());
//进程数
$work_number = 6;

//
$worker = [];

//模拟地址
$curl = [
    'https://blog.csdn.net/feiwutudou',
    'https://wiki.swoole.com/wiki/page/215.html',
    'http://fanyi.baidu.com/?aldtype=16047#en/zh/manager',
    'http://www.phpxs.com/post/8429/',
    'http://o.ngking.com/themes/mskin/login/login.jsp',
    'https://blog.csdn.net/marksinoberg/article/details/77816991'
];

//单线程模式
// foreach ($curl as $v) {
//  echo curldeta($v);
// }

//创建进程
for ($i = 0; $i < $work_number; $i++) {
    //创建多线程
    $pro = new swoole_process(function (swoole_process $work) use ($i, $curl) {
        //获取html文件
        $content = curl($curl[$i]);
        //写入管道
        $work->write($content . PHP_EOL);
    }, true);
    $pro_id = $pro->start();
    $worker[$pro_id] = $pro;
}
//读取管道内容
foreach ($worker as $v) {
    echo $v->read() . PHP_EOL;
    //file_put_contents('', $v->read());
}

//模拟爬虫
function curl($curl_arr)
{
    return file_get_contents($curl_arr);
}

//进程回收
swoole_process::wait();

echo '结束时间:' . date('H:i:s', time());